package com.gzhryc.net_marketing.cloud.goods.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.net_marketing.core.enums.EGoodsState;
import com.gzhryc.net_marketing.cloud.goods.dao.db.CloudGoodsSpace;
import com.gzhryc.net_marketing.cloud.MarketConstants;
import com.gzhryc.net_marketing.cloud.goods.dao.CloudGoodsSpaceDao;
import org.apache.commons.lang3.StringUtils;

import java.sql.SQLException;
import java.util.List;

public class CloudGoodsSpaceService {

    static Logger log = Logger.getLogger(CloudGoodsSpaceService.class);

    public static CloudGoodsSpaceService self() {
        return MultiDBTools.getService(MarketConstants.JDBC_KEY,CloudGoodsSpaceService.class, true);
    }

    CloudGoodsSpaceDao dao;

    public CloudGoodsSpaceService(String jdbcKey) {
        this.dao = new CloudGoodsSpaceDao(jdbcKey);
    }

    public List<CloudGoodsSpace> findByGoodsId(String goodsId){
        if(StringUtils.isNotBlank(goodsId)){
            Conditions conditions = new Conditions(CloudGoodsSpace.class);
            conditions.and().is("goodsId",goodsId);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public Long countByGoodsId(String goodsId){
        if(StringUtils.isNotBlank(goodsId)) {
            Conditions conditions = new Conditions(CloudGoodsSpace.class);
            conditions.and().is("goodsId", goodsId);

            try {
                return dao.count(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return 0L;
    }

    public List<CloudGoodsSpace> findPutByGoodsId(String goodsId){
        if(StringUtils.isNotBlank(goodsId)){
            Conditions conditions = new Conditions(CloudGoodsSpace.class);
            conditions.and().is("goodsId",goodsId);
            conditions.and().is("state", EGoodsState.PutOnTheShelf.index());

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public CloudGoodsSpace getByKeys(String goodsId,Long id){
        if(StringTools.isNotBlank(goodsId) && id != null){
            Conditions conditions = new Conditions(CloudGoodsSpace.class);
            conditions.and().is("goodsId",goodsId);
            conditions.and().is("id", id);

            try {
                return dao.get(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public boolean add(List<CloudGoodsSpace> goodsSpaceList){
        if(goodsSpaceList != null && goodsSpaceList.size() > 0){
            try {
                return dao.insert(goodsSpaceList) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    public boolean deleteByGoodsId(String goodsId){
        if(StringUtils.isNotBlank(goodsId)) {
            Conditions conditions = new Conditions(CloudGoodsSpace.class);
            conditions.and().is("goodsId", goodsId);

            try {
                return dao.deleteByCondition(conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }
}
